package com.hxkj.controller.wechat;

import com.hxkj.domain.po.AppletArticle;
import com.hxkj.service.wechat.AppletArticleService;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * 文章控制层
 * <p>
 * Created by zzx on 2018/6/6.
 */
@RestController
@RequestMapping("/appletArticle")
public class AppletArticleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppletArticleController.class);

    @Autowired
    private AppletArticleService appletArticleService;


    /**
     * 获取文章列表（包含文章回收站列表）
     *
     * @param article pageParam
     * @return 翻页数据
     * @throws Exception
     */
    @RequiresPermissions("article:view")
    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    public Callable<ResultPageStruct<AppletArticle>> getArticleList(@ModelAttribute AppletArticle article, @ModelAttribute PageTo pageParam) throws Exception {
        return () -> appletArticleService.getAppletArticleList(article == null ? new AppletArticle() : article, pageParam);
    }


    /**
     * 获取一条文章信息
     *
     * @param article
     * @return 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Callable<AppletArticle> getArticle(@ModelAttribute AppletArticle article) throws Exception {
        return () -> appletArticleService.getAppletArticle(article);
    }

    /**
     * 发布一篇文章
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequiresPermissions("article:add")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void saveArticle(@ModelAttribute AppletArticle article) throws Exception {
        appletArticleService.saveAppletArticle(article);
    }

    /**
     * 移除站点至回收站
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequiresPermissions("article:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public void removerArticle(@ModelAttribute AppletArticle article) throws Exception {
        appletArticleService.removerAppletArticle(article);
    }

    /**
     * 回收站 删除文章
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequiresPermissions("article:remove")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void deleteArticle(@ModelAttribute AppletArticle article) throws Exception {
        appletArticleService.deleteAppletArticle(article);
    }

    /**
     * 修改文章
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequiresPermissions("article:modify")
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public void updateArticle(@ModelAttribute AppletArticle article) throws Exception {
        appletArticleService.updateAppletArticle(article);
    }


    /**
     * 文章恢复
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequiresPermissions("article:modify")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void ArticleRecover(@ModelAttribute AppletArticle article) throws Exception {
        appletArticleService.AppletArticleRecover(article);
    }
}
